{include file="pub/header"/}
<title>添加管理员</title>
</head>
<body>
<article class="page-container">
    <form class="form form-horizontal" id="form-admin-add">
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>用户名：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="" placeholder="" id="adminName" name="uname">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>用户名称：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="" placeholder="" id="name" name="name">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>联系电话：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="" placeholder="" id="telephone" name="telephone">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>初始密码：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="password" class="input-text" autocomplete="off" value="" placeholder="密码" id="password"
                       name="password">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>确认密码：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="password" class="input-text" autocomplete="off" placeholder="确认新密码" id="password2"
                       name="password2">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">邀请码：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" autocomplete="off" placeholder="邀请码" id="vip_code"
                       name="vip_code">
            </div>
        </div>

        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>所属部门：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <span class="select-box" style="width: 200px;">
                    <input type="hidden" id="department_pid" name="department_pid">
                    <select id="department_pid1" class="select" size="1"></select>
                </span>
                <span class="select-box" style="width: 200px;">
                    <select id="department_id" name="department_id" class="select" size="1"></select>
                </span>
            </div>
        </div>

        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>角色：</label>
            <div class="formControls col-xs-8 col-sm-9">
                {foreach $roleList as $key=>$role}
                <input type="checkbox" class="input-small" onclick="showAgent('{$key}', this)" id="role-{$key}" value="{$key}" placeholder=""
                       name="role_id[]" >
                <label for="role-{$key}" class="">{$role}</label>
                {/foreach}
            </div>
        </div>

        <div class="row cl agent" style="display: none;">
            <label class="form-label col-xs-4 col-sm-3">代理商等级：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <span class="select-box" style="width: auto;">
                    <select name="agent_level" class="select">
                        <option value="1">市级代理</option>
                        <option value="2">区县级代理</option>
                    </select>
                </span>
            </div>
        </div>
        <div class="row cl agent" style="display: none;">
            <label class="form-label col-xs-4 col-sm-3">代理商类型：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <span class="select-box" style="width: auto;">
                    <select name="agent_cate" class="select">
                        <option value="1">代理</option>
                        <option value="2">兼职代理</option>
                    </select>
                </span>
            </div>
        </div>
        <div class="row cl agent" style="display: none;">
            <label class="form-label col-xs-4 col-sm-3">推广用户佣金金额：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" name="person_money" class="input input-text" value="20">
            </div>
        </div>
        <div class="row cl agent" style="display: none;">
            <label class="form-label col-xs-4 col-sm-3">动销首单佣金金额：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" name="first_order" class="input input-text" value="20">
            </div>
        </div>
        <div class="row cl agent" style="display: none;">
            <label class="form-label col-xs-4 col-sm-3">代理商区域：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <span class="select-box" style="width: auto;">
                    <select name="province_id" class="select province" onchange="getNextArea(this, 'city')">
                        <option value="">请选择</option>
                        {foreach $province as $item}
                            <option value="{$item.id}">{$item.name}</option>
                        {/foreach}
                    </select>
                </span>
                <span class="select-box" style="width: auto;">
                    <select name="city_id" class="select city" onchange="getNextArea(this, 'country')">
                        <option value="">请选择</option>
                    </select>
                </span>
                <span class="select-box" style="width: auto;">
                    <select name="country_id" class="select country">
                        <option value="">请选择</option>
                    </select>
                </span>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">描述：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <textarea name="describe" cols="" rows="" class="textarea" placeholder="说点什么...100个字符以内" dragonfly="true"></textarea>
            </div>
        </div>
        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
            </div>
        </div>
    </form>
</article>

{include file="pub/footer"/}

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript">
    function getNextArea(o, sl){
        var pid = $(o).val();
        $.getJSON("{:url('getArea')}", {pid:pid}, function(res){
            var data = res.data;
            var _l = "<option value=''>请选择</option>";
            if(sl == "city"){
                $(".country").html(_l);
            }
            $.each(data, function (i,v) {
                _l += '<option value="'+v.id+'">'+v.name+'</option>';
            });
            $("."+sl).html(_l);
        })
    }

    function showAgent(role_id, o){
        var is_show = $(o).prop("checked");
        if(role_id == 15){
            is_show?$(".agent").show():$(".agent").hide();
        }
    }


    $(function () {
        var $dep_list = {$dep_list};
        var opts = '';
        $.each($dep_list, function(i, v){
            opts += '<option value="'+i+'">'+v.name+'</option>';
        });
        $("#department_pid1").html(opts);
        var cateId = $("#department_pid1").val();
        $("#department_pid").val($dep_list[cateId].id);
        var opts1= '';
        $.each($dep_list[cateId].children, function (i1,v1) {
            opts1 += '<option value="'+v1.id+'">'+v1.name+'</option>';
        });
        $("#department_id").html(opts1);
        $("#department_pid1").on('change', function () {
            var id = $(this).val();
            $("#department_pid").val($dep_list[id].id);
            var opts2 = "";
            $.each($dep_list[id].children, function (i1,v1) {
                opts2 += '<option value="'+v1.id+'">'+v1.name+'</option>';
            });
            $("#department_id").html(opts2);
        });

        $("#form-admin-add").validate({
            rules: {
                uname: {
                    required: true,
                    minlength: 4,
                    maxlength: 10,
                    remote: '{:url("Admin/checkName")}'
                },
                password: {
                    required: true,
                    minlength: 6
                },
                password2: {
                    required: true,
                    equalTo: "#password"
                },
                "role_id[]": {
                    required: true,
                    minlength: 1
                }
            },
            messages: {
                uname: {
                    required: '用户名不能为空',
                    minlength: '最少不能少于4位',
                    maxlength: '最多不能多余10位',
                    remote: '用户名已存在'
                },
                password: {
                    required: '密码不能为空',
                    minlength: '最少不能少于6位'
                },
                "role_id[]": {
                    required: '角色必须选择',
                    minlength: '至少选择一个角色'
                }
            },
            submitHandler: function (form) {
                $(form).ajaxSubmit({
                    type: 'post',
                    url: "{:url('Admin/adminAdd')}",
                    success: function (data) {
                        if (1 == data.code) {
                            var index = parent.layer.getFrameIndex(window.name);
                            layer.msg(data.msg, {icon: 6, time: 1000});
                            setTimeout(function () {
                                parent.location.reload();
                                parent.layer.close(index);
                            }, 1000);
                        } else {
                            layer.msg(data.msg, {icon: 6, time: 1000});
                        }
                    },
                    error: function (XmlHttpRequest, textStatus, errorThrown) {
                        layer.msg('error!', {icon: 1, time: 1000});
                    }
                });
            }
        });
    });
</script>
<!--/请在上方写此页面业务相关的脚本-->
</body>
</html>